package cn.com.vrv.common;

import java.io.IOException;

import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ExceptionHandler;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;

/**
 * 抽象控制器
 * 
 * @author 赵炎
 * @version [V1.00, 2017年11月21日]
 * @see [相关类/方法]
 * @since V1.00
 */
public abstract class AbstractController
{
    protected Logger log = LoggerFactory.getLogger(this.getClass());
    
    /**
     * 异常捕捉机制
     * 
     * @param e
     * @param response
     * @throws IOException
     * @see [类、类#方法、类#成员]
     */
    @ExceptionHandler(Exception.class)
    public void hanlerException(Exception e, HttpServletResponse response)
        throws IOException
    {
        ModelMap modelMap = new ModelMap();
        if (e instanceof IllegalArgumentException)
        {
            modelMap.addAttribute("success", Boolean.FALSE);
            modelMap.addAttribute("timestamp", System.currentTimeMillis());
            modelMap.addAttribute("message", e.getMessage());
        }
        log.info("后台异常内容", JSON.toJSON(modelMap));
        response.setContentType("application/json;charset=UTF-8");
        byte[] bytes = JSON.toJSONBytes(modelMap, SerializerFeature.DisableCircularReferenceDetect);
        response.getOutputStream().write(bytes);
    }
}
